from Pinyin2Hanzi import DefaultDagParams
from Pinyin2Hanzi import dag
from Pinyin2Hanzi import all_pinyin
import itertools


def pinyin_2_hanzi(pinyinList: list, path_num: int = 10):
    dagParams = DefaultDagParams()
    # 10个候选值
    result = dag(dagParams, pinyinList, path_num=path_num, log=True)
    # result = dag(dagParams, pinyinList, log=True)
    for item in result:
        socre = item.score  # 得分
        res = item.path  # 转换结果
        print(socre, res)
        return socre, res


def show_all_ypinyin():
    y_list = []
    for pinyin in all_pinyin():
        if pinyin[0] == "y":
            y_list.append(pinyin)
    return y_list


def make_y_2_list(path_num:int = 10,threshold:int = 3):
    y_list = show_all_ypinyin()

    y_2_list = list(itertools.product(y_list, y_list))
    print(y_2_list)
    word_and_vector = []
    for y in y_2_list:
        socre, res = pinyin_2_hanzi(y, path_num)
        if abs(socre) < threshold:
            word_and_vector.append([res, socre])
    print(word_and_vector)
    with open("word_and_vector.csv", 'w', encoding="utf-8") as writer_file:
        writer_file.write(str(word_and_vector))

    return word_and_vector

if __name__ == "__main__" :
    make_y_2_list()